package com.example.educationblockchain.controller;

import com.alibaba.excel.EasyExcel;
import com.example.educationblockchain.model.*;
import com.example.educationblockchain.model.Class;
import com.example.educationblockchain.service.AdminInformationService;
import com.example.educationblockchain.service.P2PService;
import com.example.educationblockchain.util.SseEmitterServer;
import com.example.educationblockchain.websocket.P2PClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.mvc.method.annotation.SseEmitter;

import javax.crypto.BadPaddingException;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.security.NoSuchProviderException;
import java.security.spec.InvalidKeySpecException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/adminInfo")
public class AdminInformationController {

    @Autowired
    AdminInformationService adminInformationService;

    @Autowired
    SseEmitterServer sseEmitterServer;

    @Autowired
    P2PService p2PService;

    @Autowired
    P2PClient p2PClient;

    @PostMapping("/addInfo")
    @ResponseBody
    public String addInfo(@RequestBody Map<String,Object> data) throws BadPaddingException, NoSuchAlgorithmException, NoSuchPaddingException, UnsupportedEncodingException, IllegalBlockSizeException, NoSuchProviderException, InvalidKeyException, InvalidKeySpecException {

        System.out.println(data);

        adminInformationService.insertInformation(data);

        return "成功";
    }

    @PostMapping("/changeInfo")
    @ResponseBody
    public long changeInfo(@RequestBody Map<String,String> data) throws BadPaddingException, NoSuchAlgorithmException, NoSuchPaddingException, UnsupportedEncodingException, IllegalBlockSizeException, NoSuchProviderException, InvalidKeyException, InvalidKeySpecException {

        System.out.println(data);

        long index = adminInformationService.editData(data);

        return index;
    }

    @PostMapping("/listInfo")
    @ResponseBody
    public Map<String, Object> listInfo(@RequestBody Map<String,String> map){
        System.out.println(map);
        int pageNum = Integer.parseInt(map.get("pageNum"));
        String search = map.get("search");
        if(search==""){
            System.out.println("列表");
            return adminInformationService.getInfoList(pageNum);
        }else {
            System.out.println("查询列表");
            return adminInformationService.getSearchInfoList(pageNum,search);
        }
    }

    @PostMapping("/listClassInfo")
    @ResponseBody
    public Map<String, Object> listClassInfo(@RequestBody Map<String,String> map){
        System.out.println(map);
        int pageNum = Integer.parseInt(map.get("pageNum"));
        int id = Integer.parseInt(map.get("id"));
        String search = map.get("search");
        return adminInformationService.getSearchClassInfoList(pageNum,id,search);
    }

    @GetMapping("/getDetail")
    @ResponseBody
    public Map<String,Object> getDetail(long index) throws BadPaddingException, NoSuchAlgorithmException, NoSuchPaddingException, UnsupportedEncodingException, IllegalBlockSizeException, NoSuchProviderException, InvalidKeyException, InvalidKeySpecException {
        Map map = adminInformationService.getDataByIndex(index);
        return map;
    }

    @PostMapping("/getDataByPage")
    @ResponseBody
    public long getDataByPage(@RequestBody Map<String,String> map) throws BadPaddingException, NoSuchAlgorithmException, NoSuchPaddingException, UnsupportedEncodingException, IllegalBlockSizeException, NoSuchProviderException, InvalidKeyException, InvalidKeySpecException {
        long index = adminInformationService.getDataByPage(map);
        return index;
    }

    @PostMapping("/addGrade")
    @ResponseBody
    public long addGrade(@RequestBody Map<String,String> grade) throws BadPaddingException, NoSuchAlgorithmException, NoSuchPaddingException, UnsupportedEncodingException, IllegalBlockSizeException, NoSuchProviderException, InvalidKeyException, InvalidKeySpecException {

        System.out.println(grade);

        long index = adminInformationService.addGrade(grade);

        return index;
    }

    @PostMapping("/deleteGrade")
    @ResponseBody
    public long deleteGrade(@RequestBody Map<String,String> num) throws BadPaddingException, NoSuchAlgorithmException, NoSuchPaddingException, UnsupportedEncodingException, IllegalBlockSizeException, NoSuchProviderException, InvalidKeyException, InvalidKeySpecException {

        System.out.println(num);

        long index = adminInformationService.deleteGrade(num);

        return index;
    }

    @PostMapping("/editGrade")
    @ResponseBody
    public long editGrade(@RequestBody Map<String,String> grade) throws BadPaddingException, NoSuchAlgorithmException, NoSuchPaddingException, UnsupportedEncodingException, IllegalBlockSizeException, NoSuchProviderException, InvalidKeyException, InvalidKeySpecException {

        System.out.println(grade);

        long index = adminInformationService.editGrade(grade);

        return index;
    }

    @GetMapping("/getPeerData/{id}")
    public SseEmitter getPeerData(@PathVariable String id){
        //0L设置永不超时

        return sseEmitterServer.connect(id);
    }

    @PostMapping("/sseSent")
    @ResponseBody
    public String sseSent(@RequestBody Map<String,String> data){

        System.out.println(data);

        p2PService.SseSend(data);

        return "";

    }

    @PostMapping("/addPeer")
    @ResponseBody
    public String addPeer(@RequestBody Map<String,String> map) throws  InterruptedException {
        p2PClient.connectToPeer("ws://"+map.get("peer"),map.get("pwd"));
        Thread.sleep(1500);
        return "success";
    }

    @GetMapping("/getPeers")
    @ResponseBody
    public Map<String,Object> getPeers(){
        return adminInformationService.getPeersData();
    }

    @GetMapping("/peerClose")
    @ResponseBody
    public String  peerClose(String address){
        System.out.println(address);
        p2PService.webSocketClose(address);
        return "";
    }

    @GetMapping("/getCheck")
    @ResponseBody
    public Map<String,Object> getCheck(){
        return adminInformationService.getCheck();
    }

    @PostMapping("/checkBlock")
    @ResponseBody
    public long checkBlock(@RequestBody Map<String,String> map) {
        long start = Long.parseLong(map.get("start"));
        long end = Long.parseLong(map.get("end"));

        return adminInformationService.checkBlock(start,end);

    }

    @PostMapping("/getDataFromPeer")
    @ResponseBody
    public String getDataFromPeer(@RequestBody Map<String,String> map){
        System.out.println(map);
        p2PService.getDataFromPeer(map);
        return "";

    }

    @PostMapping("/changePassword")
    @ResponseBody
    public String changePassword(@RequestBody Map<String,String> map){
        System.out.println(map);

        return adminInformationService.changePassword(map);

    }

    @PostMapping("/register")
    @ResponseBody
    public String register(@RequestBody Map<String,String> map){
        System.out.println(map);

        return adminInformationService.register(map);

    }

    @GetMapping("/listCollege")
    @ResponseBody
    public List<College> listCollege(){
        return adminInformationService.listCollege();
    }

    @PostMapping("/addCollege")
    @ResponseBody
    public String addCollege(@RequestBody Map<String,String> map){
        System.out.println(map);

        return adminInformationService.addCollege(map);

    }

    @GetMapping("/deleteCollege")
    @ResponseBody
    public String deleteCollege(String index) {
        return adminInformationService.deleteCollege(index);
    }

    @PostMapping("/updateCollege")
    @ResponseBody
    public String updateCollege(@RequestBody Map<String,String> map){
        System.out.println(map);

        return adminInformationService.updateCollege(map);

    }

    @GetMapping("/getMajor")
    @ResponseBody
    public List<Major> getMajor(String index){
        return adminInformationService.getMajor(index);
    }

    @PostMapping("/addMajor")
    @ResponseBody
    public String addMajor(@RequestBody Map<String,String> map){
        System.out.println(map);

        return adminInformationService.addMajor(map);

    }

    @GetMapping("/deleteMajor")
    @ResponseBody
    public String deleteMajor(String index) {
        return adminInformationService.deleteMajor(index);
    }

    @PostMapping("/updateMajor")
    @ResponseBody
    public String updateMajor(@RequestBody Map<String,String> map){
        System.out.println(map);

        return adminInformationService.updateMajor(map);

    }

    @GetMapping("/getClass")
    @ResponseBody
    public List<Class> getClass(String index){
        return adminInformationService.getClass(index);
    }

    @PostMapping("/addClass")
    @ResponseBody
    public String addClass(@RequestBody Map<String,String> map){
        System.out.println(map);

        return adminInformationService.addClass(map);

    }

    @GetMapping("/getSelect")
    @ResponseBody
    public Map<String,Object> getSelect(){
        return adminInformationService.getSelect();
    }

    @GetMapping("/deleteClass")
    @ResponseBody
    public String deleteClass(String index) {
        return adminInformationService.deleteClass(index);
    }

    @PostMapping("/updateClass")
    @ResponseBody
    public String updateClass(@RequestBody Map<String,String> map){
        System.out.println(map);

        return adminInformationService.updateClass(map);

    }

    @GetMapping("/selectCourse")
    @ResponseBody
    public Course selectCourse(String index) {
        return adminInformationService.selectCourse(index);
    }

    @GetMapping("/listCourse")
    @ResponseBody
    public List<Course> listCourse(){
        return adminInformationService.listCourse();
    }

    @PostMapping("/addCourse")
    @ResponseBody
    public String addCourse(@RequestBody Map<String,String> map){
        System.out.println(map);

        return adminInformationService.addCourse(map);

    }

    @GetMapping("/deleteCourse")
    @ResponseBody
    public String deleteCourse(String index) {
        return adminInformationService.deleteCourse(index);
    }

    @PostMapping("/updateCourse")
    @ResponseBody
    public String updateCourse(@RequestBody Map<String,String> map){
        System.out.println(map);

        return adminInformationService.updateCourse(map);

    }

    @RequestMapping("/upload")
    @ResponseBody
    public String upload(@RequestParam("file") MultipartFile file) throws IOException, BadPaddingException, NoSuchAlgorithmException, NoSuchPaddingException, IllegalBlockSizeException, NoSuchProviderException, InvalidKeyException, InvalidKeySpecException {
        System.out.println(file.getSize());
        adminInformationService.getDataFromFile(file);
        return "";
    }

    @GetMapping("/test")
    @ResponseBody
    public String test(){
        return "test";
    }
}
